import { _decorator, animation, Animation, AnimationClip } from "cc";
import { Entity } from "../Entity";
import { AnimatorParam } from "../../enums/AnimatorParam";
import { DirectionNames } from "../../enums/Direction";
import { CocosUtils } from "../../utils/CocosUtils";
const { ccclass } = _decorator;

const BlockTypeNames = [
    "blockfront",
    "blockleft",
    "blockback",
    "blockright",
    "blockturnleft",
    "blockturnright"
];

@ccclass("BlockState")
export class BlockState extends animation.StateMachineComponent {

    public async onMotionStateEnter(controller: animation.AnimationController, motionStateStatus: Readonly<animation.MotionStateStatus>): Promise<void> {
        const direction = controller.getValue(AnimatorParam.DIRECTION);
        const action = controller.getValue(AnimatorParam.ACTION);
        const blockType = action < 4 ? BlockTypeNames[(action - direction + 4) % 4] : BlockTypeNames[action];
        const entity = controller.getComponent(Entity);
        const animation = controller.getComponentInChildren(Animation);
        const entityName = entity.name;
        const animName = `${entityName}/${action}/${DirectionNames[direction]}`;
        const atlasPath = `texture/${entityName}/${blockType}/${DirectionNames[direction]}/${blockType}`;
        await CocosUtils.playAnimation(animation, animName, atlasPath);
        entity.state = Entity.State.IDLE;
    }
}